home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / MenuBar.java < prev    next >
Text File  |  1998-09-22  |  11KB  |  349 lines

  1. /*
  2.  * @(#)MenuBar.java    1.38 98/08/21
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.util.Vector;
  17. import java.util.Enumeration;
  18. import java.awt.peer.MenuBarPeer;
  19. import java.awt.event.KeyEvent;
  20.  
  21. /**
  22.  * The <code>MenuBar</code> class encapsulates the platform's 
  23.  * concept of a menu bar bound to a frame. In order to associate 
  24.  * the menu bar with a <code>Frame</code> object, call the 
  25.  * frame's <code>setMenuBar</code> method.
  26.  * <p>
  27.  * <A NAME="mbexample"></A><!-- target for cross references -->
  28.  * This is what a menu bar might look like:
  29.  * <p>
  30.  * <img src="images-awt/MenuBar-1.gif" 
  31.  * ALIGN=center HSPACE=10 VSPACE=7>
  32.  * <p>
  33.  * A menu bar handles keyboard shortcuts for menu items, passing them 
  34.  * along to its child menus. 
  35.  * (Keyboard shortcuts, which are optional, provide the user with
  36.  * an alternative to the mouse for invoking a menu item and the
  37.  * action that is associated with it.)
  38.  * Each menu item can maintain an instance of <code>MenuShortcut</code>. 
  39.  * The <code>MenuBar</code> class defines several methods, 
  40.  * <A HREF="#shortcuts"><code>shortCuts</code></A> and 
  41.  * <A HREF="#getShortcutMenuItem"><code>getShortcutMenuItem</code></A> 
  42.  * that retrieve information about the shortcuts a given
  43.  * menu bar is managing.
  44.  *
  45.  * @version 1.38, 08/21/98
  46.  * @author Sami Shaio
  47.  * @see        java.awt.Frame
  48.  * @see        java.awt.Frame#setMenuBar(java.awt.MenuBar)
  49.  * @see        java.awt.Menu
  50.  * @see        java.awt.MenuItem
  51.  * @see        java.awt.MenuShortcut
  52.  * @since      JDK1.0
  53.  */
  54. public class MenuBar extends MenuComponent implements MenuContainer {
  55.     Vector menus = new Vector();
  56.     Menu helpMenu;
  57.  
  58.     private static final String base = "menubar";
  59.     private static int nameCounter = 0;
  60.  
  61.     /*
  62.      * JDK 1.1 serialVersionUID 
  63.      */
  64.      private static final long serialVersionUID = -4930327919388951260L;
  65.  
  66.     /**
  67.      * Creates a new menu bar.
  68.      * @since    JDK1.0
  69.      */
  70.     public MenuBar() {
  71.     }
  72.  
  73.     /**
  74.      * Construct a name for this MenuComponent.  Called by getName() when
  75.      * the name is null.
  76.      */
  77.     String constructComponentName() {
  78.         return base + nameCounter++;
  79.     }
  80.  
  81.     /**
  82.      * Creates the menu bar's peer.  The peer allows us to change the 
  83.      * appearance of the menu bar without changing any of the menu bar's 
  84.      * functionality.
  85.      */
  86.     public void addNotify() {
  87.         synchronized (getTreeLock()) {
  88.            if (peer == null) 
  89.                 peer = Toolkit.getDefaultToolkit().createMenuBar(this);
  90.  
  91.             int nmenus = getMenuCount();
  92.             for (int i = 0 ; i < nmenus ; i++) {
  93.                 getMenu(i).addNotify();
  94.             }
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * Removes the menu bar's peer.  The peer allows us to change the 
  100.      * appearance of the menu bar without changing any of the menu bar's 
  101.      * functionality.
  102.      */
  103.     public void removeNotify() {
  104.         synchronized (getTreeLock()) {
  105.             int nmenus = getMenuCount();
  106.             for (int i = 0 ; i < nmenus ; i++) {
  107.                 getMenu(i).removeNotify();
  108.             }
  109.             super.removeNotify();
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Gets the help menu on the menu bar.
  115.      * @return    the help menu on this menu bar.
  116.      * @since     JDK1.0
  117.      */
  118.     public Menu getHelpMenu() {
  119.         return helpMenu;
  120.     }
  121.  
  122.     /**
  123.      * Sets the help menu on this menu bar to be the specified menu.
  124.      * @param     m    the menu to be set as the help menu.
  125.      * @since     JDK1.0
  126.      */
  127.     public void setHelpMenu(Menu m) {
  128.         synchronized (getTreeLock()) {
  129.             if (helpMenu == m) {
  130.                 return;
  131.             }
  132.             if (helpMenu != null) {
  133.                 helpMenu.removeNotify();
  134.                 helpMenu.parent = null;
  135.             }
  136.             if (m.parent != this) {
  137.                 add(m);
  138.             }
  139.             helpMenu = m;
  140.             if (m != null) {
  141.                 m.isHelpMenu = true;
  142.                 m.parent = this;
  143.                 MenuBarPeer peer = (MenuBarPeer)this.peer;
  144.                 if (peer != null) {
  145.                 if (m.peer == null) {
  146.                     m.addNotify();
  147.                 }
  148.                 peer.addHelpMenu(m);
  149.                 }
  150.             }
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Adds the specified menu to the menu bar.
  156.      * @param        m   the menu to be added.
  157.      * @return       the menu added.
  158.      * @see          java.awt.MenuBar#remove(int)
  159.      * @see          java.awt.MenuBar#remove(java.awt.MenuComponent)
  160.      * @since        JDK1.0
  161.      */
  162.     public Menu add(Menu m) {
  163.         synchronized (getTreeLock()) {
  164.             if (m.parent != null) {
  165.                 m.parent.remove(m);
  166.             }
  167.             menus.addElement(m);
  168.             m.parent = this;
  169.  
  170.             MenuBarPeer peer = (MenuBarPeer)this.peer;
  171.             if (peer != null) {
  172.                 if (m.peer == null) {
  173.                     m.addNotify();
  174.                 }
  175.                 peer.addMenu(m);
  176.             }
  177.             return m;
  178.         }
  179.     }
  180.  
  181.     /**
  182.      * Removes the menu located at the specified 
  183.      * index from this menu bar. 
  184.      * @param        index   the position of the menu to be removed.
  185.      * @see          java.awt.MenuBar#add(java.awt.Menu)
  186.      * @since        JDK1.0
  187.      */
  188.     public void remove(int index) {
  189.         synchronized (getTreeLock()) {
  190.         MenuBarPeer peer = (MenuBarPeer)this.peer;
  191.         if (peer != null) {
  192.             Menu m = getMenu(index);
  193.             m.removeNotify();
  194.             m.parent = null;
  195.             peer.delMenu(index);
  196.         }
  197.         menus.removeElementAt(index);
  198.         }
  199.     }
  200.  
  201.     /**
  202.      * Removes the specified menu component from this menu bar.
  203.      * @param        m the menu component to be removed.
  204.      * @see          java.awt.MenuBar#add(java.awt.Menu)
  205.      * @since        JDK1.0
  206.      */
  207.     public void remove(MenuComponent m) {
  208.         synchronized(getTreeLock()) {
  209.         int index = menus.indexOf(m);
  210.         if (index >= 0) {
  211.             remove(index);
  212.         }
  213.         }
  214.     }
  215.  
  216.     /**
  217.      * Gets the number of menus on the menu bar.
  218.      * @return     the number of menus on the menu bar.
  219.      * @since      JDK1.1
  220.      */
  221.     public int getMenuCount() {
  222.         return countMenus();
  223.     }
  224.  
  225.     /**
  226.      * @deprecated As of JDK version 1.1,
  227.      * replaced by <code>getMenuCount()</code>.
  228.      */
  229.     public int countMenus() {
  230.         return menus.size();
  231.     }
  232.  
  233.     /**
  234.      * Gets the specified menu.
  235.      * @param      i the index position of the menu to be returned.
  236.      * @return     the menu at the specified index of this menu bar.
  237.      * @since      JDK1.0
  238.      */
  239.     public Menu getMenu(int i) {
  240.         return (Menu)menus.elementAt(i);
  241.     }
  242.  
  243.     /** 
  244.      * Gets an enumeration of all menu shortcuts this menu bar 
  245.      * is managing.  
  246.      * @return      an enumeration of menu shortcuts that this
  247.      *                      menu bar is managing.
  248.      * @see         java.awt.MenuShortcut
  249.      * @since       JDK1.1
  250.      */
  251.     public synchronized Enumeration shortcuts() {
  252.         Vector shortcuts = new Vector();
  253.         int nmenus = getMenuCount();
  254.         for (int i = 0 ; i < nmenus ; i++) {
  255.             Enumeration e = getMenu(i).shortcuts();
  256.             while (e.hasMoreElements()) {
  257.                 shortcuts.addElement(e.nextElement());
  258.             }
  259.         }
  260.         return shortcuts.elements();
  261.     }
  262.  
  263.     /**
  264.      * Gets the instance of <code>MenuItem</code> associated 
  265.      * with the specified <code>MenuShortcut</code> object,
  266.      * or <code>null</code> if none has been specified.
  267.      * @param        s the specified menu shortcut.
  268.      * @see          java.awt.MenuItem
  269.      * @see          java.awt.MenuShortcut
  270.      * @since        JDK1.1
  271.      */
  272.      public MenuItem getShortcutMenuItem(MenuShortcut s) {
  273.         int nmenus = getMenuCount();
  274.         for (int i = 0 ; i < nmenus ; i++) {
  275.             MenuItem mi = getMenu(i).getShortcutMenuItem(s);
  276.             if (mi != null) {
  277.                 return mi;
  278.             }
  279.         }
  280.         return null;  // MenuShortcut wasn't found
  281.      }
  282.  
  283.     /*
  284.      * Post an ACTION_EVENT to the target of the MenuPeer 
  285.      * associated with the specified keyboard event (on 
  286.      * keydown).  Returns true if there is an associated 
  287.      * keyboard event.
  288.      */
  289.     boolean handleShortcut(KeyEvent e) {
  290.         // Is it a key event?
  291.         int id = e.getID();
  292.         if (id != KeyEvent.KEY_PRESSED && id != KeyEvent.KEY_RELEASED) {
  293.             return false;
  294.         }
  295.  
  296.         // Is the accelerator modifier key pressed?
  297.         int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  298.         if ((e.getModifiers() & accelKey) == 0) {
  299.             return false;
  300.         }
  301.  
  302.         // Pass MenuShortcut on to child menus.
  303.         int nmenus = getMenuCount();
  304.         for (int i = 0 ; i < nmenus ; i++) {
  305.             Menu m = getMenu(i);
  306.             if (m.handleShortcut(e)) {
  307.                 return true;
  308.             }
  309.         }
  310.         return false;
  311.     }
  312.  
  313.     /**
  314.      * Deletes the specified menu shortcut.
  315.      * @param     s the menu shortcut to delete.
  316.      * @since     JDK1.1
  317.      */
  318.     public void deleteShortcut(MenuShortcut s) {
  319.         int nmenus = getMenuCount();
  320.         for (int i = 0 ; i < nmenus ; i++) {
  321.             getMenu(i).deleteShortcut(s);
  322.         }
  323.     }
  324.  
  325.     /* Serialization support.  Restore the (transient) parent 
  326.      * fields of Menubar menus here.
  327.      */
  328.  
  329.     private int menuBarSerializedDataVersion = 1;
  330.  
  331.     private void writeObject(java.io.ObjectOutputStream s)
  332.       throws java.lang.ClassNotFoundException,
  333.              java.io.IOException 
  334.     {
  335.       s.defaultWriteObject();
  336.     }
  337.  
  338.     private void readObject(java.io.ObjectInputStream s)
  339.       throws java.lang.ClassNotFoundException,
  340.              java.io.IOException 
  341.     {
  342.       s.defaultReadObject();
  343.       for (int i = 0; i < menus.size(); i++) {
  344.         Menu m = (Menu)menus.elementAt(i);
  345.         m.parent = this;
  346.       }
  347.     }
  348. }
  349.